package com.jse.websocket;

import java.util.Date;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.plugins.mvc.websocket.handler.SimpleWsHandler;

@IocBean
public class JseWsHandler extends SimpleWsHandler {

	@Inject
	Websocket websocket;

	private final Log log = Logs.get();

	public JseWsHandler() {
		this("wsroom:");
	}

	public JseWsHandler(String prefix) {
		super(prefix);
	}

	@Override
	public void onMessage(String message) {
		try {
			NutMap msg = Json.fromJson(NutMap.class, message);
			String action = msg.getString("action");
			if (Strings.isBlank(action))
				return;
			String room = msg.getString("room");
			switch (action) {
			case "join":
				session.getAsyncRemote().sendText(message);
				join(room);
				break;
			case "left":
				left(room);
				break;
			case "send":

				break;
			default:
				break;
			}
			log.infof("%s web socket handler on message:%", Times.sDTms2(new Date()), msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
